<?php
/**
 * Created by PhpStorm.
 * User: Peco
 * Date: 2019/9/16
 * Time: 22:15
 */

namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Banner extends Model
{
    use SoftDeletes;

    /**
     * 与模型关联的数据表
     *
     * @var string
     */
    protected $table = 'banner';

    /**
     * 主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 需要被转换成日期的属性。
     *
     * @var array
     */
    protected $dates = ['created_at', 'updated_at'];

    /**
     * @var array
     */
    protected $guarded = ['id'];

    /**
     * @var array
     */
    protected $hidden = ['deleted_at'];

    /**
     * @var bool
     */
    public $timestamps = false;

    /**
     * @var array
     */
    protected $appends = [
        'image_attachment',  //列表logo附件
        'theme_image_attachment', //展示图组附件
    ];

    /**
     * @return bool
     */
    public function getImageAttachmentAttribute()
    {
        $result = Attachment::find($this->image);
        return $result ? $result->full_path : null;
    }

    /**
     * @return |null
     */
    public function getThemeImageAttachmentAttribute(){
        $result = $this->themeImage ? Attachment::find($this->themeImage) : null;
        return $result ? $result->full_path : null;
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function attachment(){
        return $this->belongsTo(Attachment::class, 'id', 'belong_id');
    }
}
